/*
 * 本地数据存储 indexedDB
 */

define(function(require,exports,module){
	var indexedDB=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB;
	
	var localDB={
		dbName:'diary_db',
		currentVersion:1,
		db:null,
		//创建数据库
		create:function(){
			var dbRequest=indexedDB.open(this.dbName,this.currentVersion);
			var self=this;
			dbRequest.onerror=function(error){
				
			};
			dbRequest.onsuccess=function(evt){
				
			};
			dbRequest.onupgradeneeded=function(evt){self.dbVersionUpgrade(evt);};
			dbRequest.onblocked=function(){};
		},
		//若数据库不存在，则新创建
		dbVersionUpgrade:function(evt){
			if(localDB.db){
				localDB.db.close();
			}
			localDB.db=evt.target.result;
			var txn=evt.target.transaction;
			//构造表结构
			localDB.db.createObjectStore("diary",{keyPath:"Id"});
			txn.oncomplete=function(){};
			txn.onerror=function(){};
		},
		//删除数据库
		deleteDB:function(){
		  if(localDB.db){
			  localDB.db.close();
		  }
		  localDB.db=null;
		  var dbRequest=indexedDB.deleteDatabase(this.dbName);
		  dbRequest.onerror=function(){};
		  dbRequest.onsuccess=function(){};
		  dbRequest.onblocked=function(){};
		},
		//向数据库中插入数据
		addData:function(data,tableName){
			var self=this;
			var dbRequest=indexedDB.open(this.dbName,this.currentVersion);
			dbRequest.onerror=function(){};
			dbRequest.onsuccess=function(evt){
				self.db=evt.target.result;
				self._addData(data,tableName);
			};
			dbRequest.onupgradeneeded=function(evt){self.dbVersionUpgrade(evt);};
			dbRequest.onblock=function(){};
		},
		//插入数据的内部方法
		_addData:function(data,tableName){
			if(localDB.db===null){}
			var txn=localDB.db.transaction([tableName],"readwrite");
			txn.onerror=function(){};
			txn.onabort=function(){};
			txn.oncomplete=function(){};
			var myStore=txn.objectStore(tableName);
			var addResult=myStore.add(data);
			addResult.onerror=function(evt){};
		},
		//从数据库读取数据
		getAllData:function(tableName,callback){
			var self=this;
			var dbRequest=indexedDB.open(this.dbName,this.currentVersion);
			dbRequest.onerror=function(){};
			dbRequest.onsuccess=function(evt){
				self._getAllData(evt,tableName,callback);
			};
			dbRequest.onupgradeneeded=function(evt){self.dbVersionUpgrade(evt);};
			dbRequest.onblocked=function(){};
		},
		//读取数据的内部方法
		_getAllData:function(evt,tableName,callback){
			var result=[];
			localDB.db=evt.target.result;
			if(localDB.db.objectStoreNames.length===0){
				self.deleteDB();
			}else{
				//读取数据操作
				var txn=localDB.db.transaction([tableName],"readyonly");
				txn.onerror=function(){};
				txn.onabort=function(){};
				txn.oncomplete=function(){
					callback&&callback(result);
				};
				var myCursorRequest=txn.objectStore(tableName).openCursor();
				myCursorRequest.onsuccess=function(e){
					var cursor=e.target.result;
					if(cursor){
						var value=cursor.value;
						result.push(value);
						cursor.continue();
					}
				}
			}
			
		},
		//清空数据库中某个objectstore的数据
		clearAllData:function(tableName,callback){
			var self=this;
			var dbRequest=indexedDB.open(this.dbName,this.currentVersion);
			dbRequest.onerror=function(){};
			dbRequest.onsuccess=function(evt){
				self._clearAllData(evt,tableName,callback);
			};
			dbRequest.onupgradeneeded=function(evt){self.dbVersionUpgrade(evt);};
			dbRequest.onblocked=function(){};
		},
		//内部清空数据
		_clearAllData:function(evt,tableName,callback){
			localDB.db=evt.target.result;
			var txn=localDB.db.transaction([tableName],"readwrite");
			txn.onerror=function(){};
			txn.onabort=function(){};
			txn.oncomplete=function(){
			  callback&&callback();	
			};
			var myStore=txn.objectStore(tableName).clear();
			myStore.onsuccess=function(){};
			myStore.onerror=function(){};
		},
	};
	  localDB.create();
	 
	 module.exports=localDB; 
	
});